package com.wxzz.elearing.comment.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.amqp.producer.CommentProducer;
import com.wxzz.elearing.comment.entity.Comment;
import com.wxzz.elearing.comment.entity.CommunityComment;
import com.wxzz.elearing.comment.entity.CommunityQuestionsAnswersList;
import com.wxzz.elearing.comment.mapper.CommunityQuestionsAnswersListMapper;
import com.wxzz.elearing.comment.service.ICommunityGiveLikeService;
import com.wxzz.elearing.comment.service.IGiveLikeService;
import com.wxzz.elearning.common.dto.SystemMessageMqDto;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;


@Service
public class CommunityGiveLikeServiceImpl implements ICommunityGiveLikeService {


    /**
     * 点赞状态
     */
    public final static String COMMUNITYGIVELIKESTATUS = "CommunityGiveLikeStatus";

    /**
     * 点赞数量
     */
    public final static String COMMUNITYGIVELIKECOUNT = "CommunityGiveLikeCount";


    /**
     * 热度
     */
    public final static String COMMUNITYHEATCOUNT = "CommunityHeatCount";

    /**
     * 草稿
     */
    public final static String COMMUNITYDRAFTINFO = "CommunityDraftInfo";

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private CommunityQuestionsAnswersListMapper communityQuestionsAnswersListMapper;

    @Autowired
    private CommentProducer commentProducer;

    public ReturnVO giveLike(CommunityComment comment){
        //点赞的用户id
        Long wxUserId = comment.getWxUserId();
        //用户点赞的文章id
        Long articleId = comment.getArticleId();
        Map<String,String> giveLikeStatus = redisTemplate.opsForHash().entries(COMMUNITYGIVELIKESTATUS);
        String userIdOfArt=wxUserId + ":" + articleId;
        if(giveLikeStatus.size()!=0){
            String s = giveLikeStatus.get(userIdOfArt);
            if(StringUtil.isNotBlank(s)){
                //如果已经点赞就取消点赞
                if(s.equals("1")){
                    redisTemplate.opsForHash().put(COMMUNITYGIVELIKESTATUS,userIdOfArt,"0");
                    return delGiveLike(articleId);
                }else{
                    redisTemplate.opsForHash().put(COMMUNITYGIVELIKESTATUS,userIdOfArt,"1");
                    return addGiveLike(articleId);
                }
            }else{
                redisTemplate.opsForHash().put(COMMUNITYGIVELIKESTATUS,userIdOfArt,"1");
                return addGiveLike(articleId);
            }
        }else{
            addGiveLike(articleId);
            redisTemplate.opsForHash().put(COMMUNITYGIVELIKESTATUS,userIdOfArt,"1");
        }

        return new ReturnVO("点赞成功!",null);
    }

    public ReturnVO addGiveLike(Long articleId){
        String artCount=articleId + ":Communitycount";
        Map<String,String> entries = redisTemplate.opsForHash().entries(COMMUNITYGIVELIKECOUNT);
        if(entries.size()!=0){
            String s = entries.get(artCount);
            if(StringUtil.isNotBlank(s)){
                int i = Integer.parseInt(s);
                i+=1;
                redisTemplate.opsForHash().put(COMMUNITYGIVELIKECOUNT,artCount,String.valueOf(i));
            }else{
                redisTemplate.opsForHash().put(COMMUNITYGIVELIKECOUNT,artCount,"1");
            }
        }else{
            redisTemplate.opsForHash().put(COMMUNITYGIVELIKECOUNT,artCount,"1");
        }

        CommunityQuestionsAnswersList communityQuestionsAnswersList = communityQuestionsAnswersListMapper.selectById(articleId);
        //todo 小程序订阅
        SystemMessageMqDto systemMessageMqDto = new SystemMessageMqDto();
        systemMessageMqDto.setMessageDetail("您的文章"+communityQuestionsAnswersList.getPublishContent()+"收到点赞");
        systemMessageMqDto.setMessageTitle("社区");
        systemMessageMqDto.setWxUserIds(new ArrayList<Long>() {
            {
                add(communityQuestionsAnswersList.getPublishPersonId());
            }
        });
        commentProducer.ackMQSender(JSONObject.toJSONString(systemMessageMqDto));
        return new ReturnVO("点赞成功",null);
    }
    public ReturnVO delGiveLike(Long articleId){
        String artCount=articleId + ":Communitycount";
        Map<String,String> entries = redisTemplate.opsForHash().entries(COMMUNITYGIVELIKECOUNT);
        if(entries.size()!=0){
            String s = entries.get(artCount);
            if(StringUtil.isNotBlank(s)){
                int i = Integer.parseInt(s);
                i-=1;
                redisTemplate.opsForHash().put( COMMUNITYGIVELIKECOUNT,artCount,String.valueOf(i));
            }else{
                redisTemplate.opsForHash().put( COMMUNITYGIVELIKECOUNT,artCount,"1");
            }
        }else{
            redisTemplate.opsForHash().put(COMMUNITYGIVELIKECOUNT,artCount,"1");
        }
        return new ReturnVO("取消点赞成功",null);
    }

}
